﻿using System.Collections.Generic;
using System.Linq;

namespace Froggy.Stocks.Analysis.Crawlers
{
    public class AvgArithmeticValueAnalysis: IAnalysis
    {
        public AvgArithmeticValueAnalysis()
        {
        }

        private List<double> _CloseValues;
        private int _NumberOfPeriods;

        public int Period
        {
            get { return _NumberOfPeriods; }
            set 
            { 
                _CloseValues = new List<double>(value);
                _NumberOfPeriods = value; 
            }
        }

        public IPattern Analyse(IQuotationSnapshot qs)
        {
            if (Period >= _CloseValues.Count)
            {
                _CloseValues.RemoveAt(0);
            }
            _CloseValues.Add(qs.CloseValue);
            var pattern = new AvgArithmeticValuePattern {QuotationShapshot = qs, Media = _CloseValues.Average()};
            return pattern;
        }
    }
}